package cn.itcast.classSM.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.classSM.domain.CheckMessage;
import cn.itcast.classSM.domain.CheckTimeDul;
import cn.itcast.classSM.domain.Clazz;
import cn.itcast.classSM.domain.MainCheckMessage;
import cn.itcast.classSM.service.AdministratorService;
import cn.itcast.classSM.utils.FileUtils;
import cn.itcast.classSM.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class AdministratorAction extends BaseAction<Clazz> {
	@Autowired
	private AdministratorService administratorService;

	private Clazz clazz = new Clazz();

	// 添加班级
	@Action(value = "clazz_update", results = { @Result(name = "success", type = "redirect", location = "") })
	public String addClazz() {
		administratorService.add(clazz);
		return "success";

	}

	// 删除班级(根据id删除)
	@Action(value = "clazz_delete", results = { @Result(name = "success", type = "redirect", location = "") })
	public String delClazz(Clazz clazz) {
		administratorService.delete(clazz);
		return "success";
	}

	// 查找所有班级信息
	@Action(value = "clazz_findAll", results = { @Result(name = "success", type = "redirect", location = "") })
	public String findClazz() {
		List<Clazz> list = administratorService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getValueStack(request).set("list", list);
		return "success";
	}
	//检查时间
	private String checkTime;
	//班级ID
	private String clazzID;

	public void setCheckTime(String checkTime) {
		this.checkTime = checkTime;
	}
	public void setClazzID(String clazzID) {
		this.clazzID = clazzID;
	}

	@Action("report_exportXls")
	public String exportXls() throws Exception {
		// 查询出 满足当前条件 结果数据
		List<CheckMessage> byMonth = new ArrayList<>();
		if(checkTime!=null){	
			byMonth= administratorService.findByMonth(checkTime);
			// 生成Excel文件
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
			HSSFSheet sheet = hssfWorkbook.createSheet("黑马双元课堂班级日常管理表");
			// 表头
			HSSFRow headRow = sheet.createRow(0);
			headRow.createCell(0).setCellValue("班级");
			headRow.createCell(1).setCellValue("班级人数");
			headRow.createCell(2).setCellValue("教室");
			headRow.createCell(3).setCellValue("时间");
			headRow.createCell(4).setCellValue("应到人数");
			headRow.createCell(5).setCellValue("出勤人数");
			headRow.createCell(6).setCellValue("迟到（姓名）");
			headRow.createCell(7).setCellValue("请假（姓名）");
			headRow.createCell(8).setCellValue("穿拖鞋");
			headRow.createCell(9).setCellValue("睡觉");
			headRow.createCell(10).setCellValue("手机袋");
			headRow.createCell(11).setCellValue("卫生");
			headRow.createCell(12).setCellValue("学习氛围");
			headRow.createCell(13).setCellValue("突发状况的处理");
			// 表格数据
			for (CheckMessage cm : byMonth) {
				for (MainCheckMessage mcm : cm.getMainCheckMessages()) {
					HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
					dataRow.createCell(0).setCellValue(mcm.getMcmClazz().getCname());
					dataRow.createCell(1).setCellValue(mcm.getMcmClazz().getCnumber());
					dataRow.createCell(2).setCellValue(mcm.getMcmClazz().getClassRoom().getRnumber());
					for (CheckTimeDul checkTimeDul : mcm.getCheckTimeDuls()) {
						dataRow.createCell(3).setCellValue(checkTimeDul.getTimeDual());
						dataRow.createCell(4).setCellValue(mcm.getShouldArrive());
						dataRow.createCell(5).setCellValue(mcm.getFactArrive());
						dataRow.createCell(6).setCellValue(mcm.getLateName());
						dataRow.createCell(7).setCellValue(mcm.getLeaveName());
						dataRow.createCell(8).setCellValue(mcm.getWearSlippers());
						dataRow.createCell(9).setCellValue(mcm.getSleeping());
						dataRow.createCell(10).setCellValue(mcm.getPhoneNumber());
						dataRow.createCell(11).setCellValue(mcm.getHygiene());
						dataRow.createCell(12).setCellValue(mcm.getAtmosphere());
						dataRow.createCell(13).setCellValue(mcm.getRemark());
					}
				}
			}
			// 下载导出
			// 设置头信息
			ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
			String filename = "查班信息.xls";
			String agent = ServletActionContext.getRequest().getHeader("user-agent");
			filename = FileUtils.encodeDownloadFilename(filename, agent);
			ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename);
			
			ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
			hssfWorkbook.write(outputStream);
			
			// 关闭
			hssfWorkbook.close();
			
			return NONE;
		}else{
			//时间错误
			return null;
		}
	}
	@Action("report_exportXls2")
	public String exportXls2() throws Exception {
		// 查询出 满足当前条件 结果数据
		List<MainCheckMessage> byMonth = administratorService.findByClazzMonth(checkTime,clazz.getCid());
		
		// 生成Excel文件
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet sheet = hssfWorkbook.createSheet("黑马双元课堂班级日常管理表");
		// 表头
		HSSFRow headRow = sheet.createRow(0);
		headRow.createCell(0).setCellValue("班级");
		headRow.createCell(1).setCellValue("班级人数");
		headRow.createCell(2).setCellValue("教室");
		headRow.createCell(3).setCellValue("时间");
		headRow.createCell(4).setCellValue("应到人数");
		headRow.createCell(5).setCellValue("出勤人数");
		headRow.createCell(6).setCellValue("迟到（姓名）");
		headRow.createCell(7).setCellValue("请假（姓名）");
		headRow.createCell(8).setCellValue("穿拖鞋");
		headRow.createCell(9).setCellValue("睡觉");
		headRow.createCell(10).setCellValue("手机袋");
		headRow.createCell(11).setCellValue("卫生");
		headRow.createCell(12).setCellValue("学习氛围");
		headRow.createCell(13).setCellValue("突发状况的处理");
		// 表格数据
		for (MainCheckMessage mcm : byMonth) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(mcm.getMcmClazz().getCname());
			dataRow.createCell(1).setCellValue(mcm.getMcmClazz().getCnumber());
			dataRow.createCell(2).setCellValue(mcm.getMcmClazz().getClassRoom().getRnumber());
			for (CheckTimeDul checkTimeDul : mcm.getCheckTimeDuls()) {
				dataRow.createCell(3).setCellValue(checkTimeDul.getTimeDual());
				dataRow.createCell(4).setCellValue(mcm.getShouldArrive());
				dataRow.createCell(5).setCellValue(mcm.getFactArrive());
				dataRow.createCell(6).setCellValue(mcm.getLateName());
				dataRow.createCell(7).setCellValue(mcm.getLeaveName());
				dataRow.createCell(8).setCellValue(mcm.getWearSlippers());
				dataRow.createCell(9).setCellValue(mcm.getSleeping());
				dataRow.createCell(10).setCellValue(mcm.getPhoneNumber());
				dataRow.createCell(11).setCellValue(mcm.getHygiene());
				dataRow.createCell(12).setCellValue(mcm.getAtmosphere());
				dataRow.createCell(13).setCellValue(mcm.getRemark());
			}
		}
		
		// 下载导出
		// 设置头信息
		ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
		String filename = "查班信息.xls";
		String agent = ServletActionContext.getRequest().getHeader("user-agent");
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename);
		
		ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
		hssfWorkbook.write(outputStream);
		
		// 关闭
		hssfWorkbook.close();
		
		return NONE;
	}

}
